package cn.tj212.xiaoyin.wechat.controller;

import cn.tj212.xiaoyin.common.page.TailPage;
import cn.tj212.xiaoyin.common.web.SessionContext;
import cn.tj212.xiaoyin.core.course.domain.Course;
import cn.tj212.xiaoyin.core.course.domain.CourseComment;
import cn.tj212.xiaoyin.core.course.domain.CourseSection;
import cn.tj212.xiaoyin.core.course.service.ICourseCommentService;
import cn.tj212.xiaoyin.core.course.service.ICourseSectionService;
import cn.tj212.xiaoyin.core.course.service.ICourseService;
import cn.tj212.xiaoyin.core.user.domain.UserCourseSection;
import cn.tj212.xiaoyin.core.user.service.IUserCourseSectionService;
import cn.tj212.xiaoyin.wechat.business.IPortalBusiness;
import cn.tj212.xiaoyin.wechat.vo.CourseSectionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private ICourseService courseService;

    @Autowired
    private IPortalBusiness portalBusiness;

    @Autowired
    private ICourseCommentService courseCommentService;

    @Autowired
    private ICourseSectionService courseSectionService;

    @Autowired
    private IUserCourseSectionService userCourseSectionService;

    @RequestMapping("/read")
    public ModelAndView read(Long id){
        ModelAndView mv=new ModelAndView("read");

        //课程基本信息
        Course course=courseService.getById(id);
        if (null==course){
            return new ModelAndView("/error/404");
        }
        mv.addObject("course",course);

        //课程章节
        List<CourseSectionVo> chaptSections=this.portalBusiness.queryCourseSection(id);
        mv.addObject("chaptSections",chaptSections);
        return mv;
    }

    /**
     * 课程视频学习
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/video")
    public ModelAndView video(HttpServletRequest request,Long id){
        ModelAndView mv=new ModelAndView("video");

        //课程基本信息
        CourseSection courseSection=courseSectionService.getById(id);
        if (null==courseSection){
            return new ModelAndView("/error/404");
        }
        //学习记录
        Long userId= SessionContext.getWxUserId(request);
        if (null!=userId){
            //获取学习记录
            UserCourseSection userCourseSection=new UserCourseSection();
            userCourseSection.setUserId(id);
            userCourseSection.setCourseId(courseSection.getCourseId());
            userCourseSection.setSectionId(courseSection.getId());
            UserCourseSection result=userCourseSectionService.queryLatest(userCourseSection);
            //如果没有学过这门课则添加学习记录
            if (null==result){
                userCourseSection.setCreateTime(new Date());
                userCourseSection.setCreateUser(SessionContext.getWxUsername(request));
                userCourseSection.setUpdateTime(new Date());
                userCourseSection.setUpdateUser(SessionContext.getWxUsername(request));
                userCourseSectionService.createSelectivity(userCourseSection);
            }else{
                result.setUpdateTime(new Date());
                userCourseSectionService.update(result);
            }
        }
        return mv;
    }

    /**
     * 课程评论分页
     * @return
     */
    @RequestMapping("/comment")
    public ModelAndView comment(CourseComment queryEntity, TailPage<CourseComment> page){
        ModelAndView mv=new ModelAndView("comment");
        TailPage<CourseComment> commentPage=this.courseCommentService.queryPage(queryEntity, page);
        mv.addObject("page",commentPage);
        return mv;
    }

}
